#include "contextTemplate.h"

const char *kIntNumberFlag = "-i";
const char *kIntNumberFlagLong = "-intprop";
const char *kDoubleNumberFlag = "-f";
const char *kDoubleNumberFlagLong = "-floatprop";
const char *kBoolFlag = "-b";
const char *kBoolFlagLong = "-boolprop";
const char *kHelpFlag = "-h";
const char *kHelpFlagLong = "-help";

const char *contextHelpText = "\n----------- \n\
use -i specify int property, \n\
use -f specify float property, \n\
use -b specify bool property.\n\
-----------";

const char *toolHelp = "click and drag to set property";
const char *toolTitle = "context template tool";

contextTemplate::contextTemplate() {
	setTitleString(toolTitle);

	// Tell the context which XPM to use so the tool can properly
	// be a candidate for the 6th position on the toolbar.
	setImage("contextTemplateTool.xpm", MPxContext::kImage1);
}

contextTemplate::~contextTemplate() {}

void contextTemplate::toolOnSetup(MEvent & event)
{
	setHelpString(toolHelp);
}

MStatus contextTemplate::doPress(MEvent &event,
                                 MHWRender::MUIDrawManager &drawMgr,
                                 const MHWRender::MFrameContext &context) {
  MGlobal::displayInfo("in do press function.");
  // Get the marquee's new end position.

  event.getPosition(startPos_x, startPos_y);

  return MS::kSuccess;
}

MStatus contextTemplate::doRelease(MEvent &event,
                                   MHWRender::MUIDrawManager &drawMgr,
                                   const MHWRender::MFrameContext &context) {
  MGlobal::displayInfo("in do release function.");

	contextTemplateTool *toolcmd = (contextTemplateTool*) newToolCommand();

  return MS::kSuccess;
}

MStatus contextTemplate::doDrag(MEvent &event,
                                MHWRender::MUIDrawManager &drawMgr,
                                const MHWRender::MFrameContext &context) {
  MGlobal::displayInfo("in do drag function.");

  event.getPosition(endPos_x, endPos_y);

  // Draw the marquee at its new position.
  // Always drawn last so no need for complicated XOR
  drawMgr.beginDrawable();
  drawMgr.setColor(MColor(0.3f, 1.0f, 0.0f));

  drawMgr.line2d(MPoint(startPos_x, startPos_y), MPoint(startPos_x, endPos_y));
  drawMgr.line2d(MPoint(endPos_x, startPos_y), MPoint(endPos_x, endPos_y));
  drawMgr.line2d(MPoint(endPos_x, endPos_y), MPoint(startPos_x, endPos_y));
  drawMgr.line2d(MPoint(endPos_x, startPos_y), MPoint(startPos_x, startPos_y));

  drawMgr.endDrawable();

  return MS::kSuccess;
}

void contextTemplate::getClassName(MString &name) const {
  name.set("contextTemplate");
}

void contextTemplate::setIntProperty(int new_property)
{
	intProperty = new_property;
}

void contextTemplate::setDoubleProperty(float new_property)
{
	doubleProperty = new_property;
}

void contextTemplate::setBoolProperty(bool new_property)
{
	boolProperty = new_property;
}

int contextTemplate::getIntProperty()
{
	return intProperty;
}

float contextTemplate::getDoubleproperty()
{
	return doubleProperty;
}

bool contextTemplate::getBoolproperty()
{
	return boolProperty;
}

//=============================================

contextTemplateCmd::contextTemplateCmd() {}

contextTemplateCmd::~contextTemplateCmd() {}

MStatus contextTemplateCmd::doEditFlags() {
	MStatus status;
	MArgParser argData = parser();
	
	if (argData.isFlagSet(kIntNumberFlag)) {
		int int_prop;
		status = argData.getFlagArgument(kIntNumberFlag, 0, int_prop);
		if (!status) {
			status.perror("int_number flag parsing failed.");
			return status;
		}
		context->setIntProperty(int_prop);
	}
	if (argData.isFlagSet(kDoubleNumberFlag)) {
		int float_prop;
		status = argData.getFlagArgument(kDoubleNumberFlag, 0, float_prop);
		if (!status) {
			status.perror("int_number flag parsing failed.");
			return status;
		}
		context->setIntProperty(float_prop);
	}
	if (argData.isFlagSet(kBoolFlag)) {
		bool bool_prop;
		status = argData.getFlagArgument(kBoolFlag, 0, bool_prop);
		if (!status) {
			status.perror("upsideDown flag parsing failed.");
			return status;
		}
		context->setBoolProperty(bool_prop);
	}

	return MS::kSuccess;

}

MStatus contextTemplateCmd::doQueryFlags() {
	MStatus status;
	MArgParser argData = parser();

	if (argData.isFlagSet(kIntNumberFlag)) {
		setResult((int) context->getIntProperty());
	}
	if (argData.isFlagSet(kDoubleNumberFlag)) {
		setResult((double) context->getDoubleproperty());
	}
	if (argData.isFlagSet(kBoolFlag)) {
		setResult(context->getBoolproperty());
	}
	return MS::kSuccess;
}

MPxContext *contextTemplateCmd::makeObj() {
  context = new contextTemplate();
  return context;
}

MStatus contextTemplateCmd::appendSyntax() {
	MStatus status;
	MSyntax  my_syntax = syntax(&status);

	status = my_syntax.addFlag(kIntNumberFlag, kIntNumberFlagLong, MSyntax::kLong);
	if (MStatus::kSuccess != status)
	{
		return status;
	}

	status = my_syntax.addFlag(kDoubleNumberFlag, kDoubleNumberFlagLong, MSyntax::kDouble);
	if (MStatus::kSuccess != status)
	{
		return status;
	}
	status = my_syntax.addFlag(kBoolFlag, kBoolFlagLong, MSyntax::kBoolean);
	if (MStatus::kSuccess != status)
	{
		return status;
	}
	return status;
}

void *contextTemplateCmd::creator() { 
	return new contextTemplateCmd; 
}

//=================================

contextTemplateTool::contextTemplateTool() {}

contextTemplateTool::~contextTemplateTool() {}

void * contextTemplateTool::creator()
{
	return new contextTemplateTool;
}

MSyntax contextTemplateTool::newSyntax()
{
	MSyntax  new_syntax;

	new_syntax.addFlag(kIntNumberFlag, kIntNumberFlagLong, MSyntax::kLong);
	new_syntax.addFlag(kDoubleNumberFlag, kDoubleNumberFlagLong, MSyntax::kDouble);
	new_syntax.addFlag(kBoolFlag, kBoolFlagLong, MSyntax::kBoolean);

	return new_syntax;
}

MStatus contextTemplateTool::parseArgs(const MArgList &args)
{
	MStatus status;
	MArgDatabase argData(syntax(), args);

	if (argData.isFlagSet(kIntNumberFlag)) {
		int tmp;
		status = argData.getFlagArgument(kIntNumberFlag, 0, tmp);
		if (!status) {
			status.perror("kIntNumberFlag flag parsing failed");
			return status;
		}
		intProperty = tmp;
	}

	if (argData.isFlagSet(kDoubleNumberFlag)) {
		double tmp;
		status = argData.getFlagArgument(kDoubleNumberFlag, 0, tmp);
		if (!status) {
			status.perror("kFloatNumberFlag flag parsing failed");
			return status;
		}
		doubleProperty = tmp;
	}

	if (argData.isFlagSet(kBoolFlag)) {
		bool tmp;
		status = argData.getFlagArgument(kBoolFlag, 0, tmp);
		if (!status) {
			status.perror("kBoolFlag flag parsing failed");
			return status;
		}
		boolProperty = tmp;
	}

	return MS::kSuccess;
}

MStatus contextTemplateTool::doIt(const MArgList &args) {
	MStatus status;
	MGlobal::displayInfo("in tool doIt");
	return status;
}

MStatus contextTemplateTool::cancel() {
	MStatus status;
	MGlobal::displayInfo("in tool cancel");
	return status;
}

MStatus contextTemplateTool::finalize() {
 	MStatus status;
  MGlobal::displayInfo("in tool finalize");
  return status;
}

MStatus contextTemplateTool::undoIt() {
 	MStatus status;
  MGlobal::displayInfo("in tool undoIt");
	return status;
}

MStatus contextTemplateTool::redoIt() {
	MStatus status;
  MGlobal::displayInfo("in tool finalize");
	return status;
}

bool contextTemplateTool::isUndoable() const {
	return true;
}

bool contextTemplateTool::hasSyntax() const {
	return true;
}
